<?php

class Onlineaide extends Controller {

	function Onlineaide()
	{
		parent::Controller();
		$this->error = "";
	}
	
	function index()
	{	
		$data['error'] = $this->session->flashdata('error');
		$data['mywebtitle'] = 'DCSOA';
		$data['base'] = $this->config->item('base_url');
		$data['css']  = $this->config->item('css');
		$data['message'] = "&nbsp;&nbsp;DCS Online Aide (DCSOA) is a web-based tool open to all students and faculty of the Department of Computer Science, College of Engineering, UP Diliman. DCSOA contains different data on the courses offered by the Department of Computer Science and is aimed at providing solutions to a user's different academic needs.";
		$this->load->view('onlineaide_view', $data);
	}

	function user_login()
	{
		$this->load->model('User');
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$user = $this->User->login_authenticate($username, sha1($password));
		if($user) {
			$this->session->set_userdata(array('status'=>'OK', 'user'=>$username, 'admin'=>$user['Admin']));
			redirect('user_control/home');
		} else {
		//wrong username/password
			$this->session->set_flashdata('error', 'Error in username or password.');
			$this->error = "Error in username and password";
			redirect('onlineaide/index');
		}
	}

	function register()
	{
		$this->load->helper('captcha');
		$aCaptchaCfg = array(
            //'word'          => 'myrandomword',    //default: random()
            'length'        => 6,                         //default: 5
            'img_path'   => './captcha/',   //no default !
            'img_url'       => $this->config->item('base_url').'/captcha/',  // no default!
            'font_path'  => './system/fonts/', // default: ./system/fonts/
            'fonts'         => array('VeraMono.ttf', 'Vera.ttf'), // default: texb.ttf
            'font_size'     => 25,      // default: 18
            'img_width'  => '180',  // default: 170
            'img_height' => '60',   // default: 60
            'expiration' => 7200 // default: 7200
            ); $aCaptcha = create_captcha($aCaptchaCfg);
		
		$data['aCaptcha'] = $aCaptcha;
		$data['mywebtitle'] = 'DCSOA Registration';
		$data['base'] = $this->config->item('base_url');
		$data['css']  = $this->config->item('css');
		$data['message'] = "&nbsp;&nbsp;DCS Online Aide (DCSOA) is a web-based tool open to all students and faculty of the Department of Computer Science, College of Engineering, UP Diliman. DCSOA contains different data on the courses offered by the Department of Computer Science and is aimed at providing solutions to a user's different academic needs.";
		$this->load->view('register_view', $data);
	}

	function username_check($username)
	{
		$this->load->model('User');
		$exists = $this->User->reg_authenticate_username($username);
		if($exists != 0) {
			$this->validation->set_message('username_check', 'The %s is already existing.');
			return FALSE;
		} else { return TRUE; }
	}

	function email_check($email)
	{
		$this->load->model('User');
		$exists = $this->User->reg_authenticate_email($email);
		if($exists != 0) {
			$this->validation->set_message('email_check', 'The %s is already existing.');
			return FALSE;
		} else { return TRUE; }
	}

	function user_registration()
	{	
		$this->load->library('validation');
		$this->validation->set_error_delimiters('<div class="error">', '</div>');

		$rules['username_regf'] = "required|alpha_dash|min_length[4]|max_length[15]|callback_username_check";
		$rules['password_regf'] = "required|matches[conf_password_regf]|min_length[6]|max_length[20]";
		$rules['conf_password_regf'] = "required";
		$rules['first_name_regf'] = "required|alpha_dash_space";
		$rules['middle_name_regf'] = "alpha_dash_space";
		$rules['last_name_regf'] = "required|alpha_dash_space";
		$rules['address_regf'] = "alpha_dash_space";
		$rules['email_regf'] = "required|valid_email|callback_email_check";
		$rules['contact_number_regf'] = "numeric|min_length[7]";
		$rules['captcha'] = "required|matches[word]";
		$rules['word'] = "required";

		$fields['word'] = "captcha image";
		$fields['captcha'] = "captcha text";
		$fields['username_regf'] = "username";
		$fields['password_regf'] = "password";
		$fields['conf_password_regf'] = "confirm password";
		$fields['first_name_regf'] = "first name";
		$fields['middle_name_regf'] = "middle name";
		$fields['last_name_regf'] = "last name";
		$fields['address_regf'] = "address";
		$fields['email_regf'] = "email address";
		$fields['contact_number_regf'] = "Your contact number";

		$this->validation->set_fields($fields);

		$this->validation->set_rules($rules);
		if ($this->validation->run() == FALSE)
		{
			$this->register();
		} else {
			$this->load->model('User');
			$username = $this->input->post('username_regf');
			$email = $this->input->post('email_regf');
			//$this->load->view('new_view', array('e'=>$email, 'f'=>$username));
			//$username_exists = $this->User->reg_authenticate_username($username);
			//$email_exists = $this->User->reg_authenticate_email($email);
			//if ($username_exists == 0 && $email_exists == 0)
			//{
				$this->load->model('User');
				$array = array('Password'=>sha1($this->input->post('password_regf')), 'Username'=>$this->input->post('username_regf'), 'LastName'=>$this->input->post('last_name_regf'), 'MiddleName'=>$this->input->post('middle_name_regf'), 'FirstName'=>$this->input->post('first_name_regf'), 'Address'=>$this->input->post('address_regf'), 'Email'=>$this->input->post('email_regf'), 'ContactNo'=>$this->input->post('contact_number_regf'));

				$this->User->register($array);
				$data['mywebtitle'] = 'DCSOA';
				$data['base'] = $this->config->item('base_url');
				$data['css']  = $this->config->item('css');
				$data['message'] = "&nbsp;&nbsp;DCS Online Aide (DCSOA) is a web-based tool open to all students and faculty of the Department of Computer Science, College of Engineering, UP Diliman. DCSOA contains different data on the courses offered by the Department of Computer Science and is aimed at providing solutions to a user's different academic needs.";
				$this->load->view('success_view', $data);
			//} else
			/*{	
				$error = 0;
				if($username_exists > 0) {
					$error += 1;
				}
				if($email_exists > 0) {
					$error += 2;
				}
				$data['mywebtitle'] = 'DCSOA Registration';
				$data['base'] = $this->config->item('base_url');
				$data['css']  = $this->config->item('css');
				$data['message'] = "&nbsp;&nbsp;DCS Online Aide (DCSOA) is a web-based tool open to all students and faculty of the Department of Computer Science, College of Engineering, UP Diliman. DCSOA contains different data on the courses offered by the Department of Computer Science and is aimed at providing solutions to a user's different academic needs.";
				$data['error'] = $error;
				$this->load->view('e_register_view', $data);	
			}*/
		}
	}
}
?>
